package com.ruoyi.system.controller;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.Expend;
import com.ruoyi.system.domain.TeaShopEmployee;
import com.ruoyi.system.service.IExpendService;
import com.ruoyi.system.service.ITeaShopEmployeeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SendSalary;
import com.ruoyi.system.service.ISendSalaryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工资发放Controller
 * 
 * @author ruoyi
 * @date 2024-05-16
 */
@Controller
@RequestMapping("/system/sendSalary")
public class SendSalaryController extends BaseController
{
    private String prefix = "system/sendSalary";

    @Autowired
    private ISendSalaryService sendSalaryService;
    @Autowired
    private IExpendService expendService;
    @Autowired
    private ITeaShopEmployeeService teaShopEmployeeService;
    @RequiresPermissions("system:sendSalary:view")
    @GetMapping()
    public String sendSalary()
    {
        return prefix + "/sendSalary";
    }

    /**
     * 查询工资发放列表
     */
    @RequiresPermissions("system:sendSalary:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SendSalary sendSalary)
    {
        startPage();
        List<SendSalary> list = sendSalaryService.selectSendSalaryList(sendSalary);
        return getDataTable(list);
    }

    /**
     * 导出工资发放列表
     */
    @RequiresPermissions("system:sendSalary:export")
    @Log(title = "工资发放", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SendSalary sendSalary)
    {
        List<SendSalary> list = sendSalaryService.selectSendSalaryList(sendSalary);
        ExcelUtil<SendSalary> util = new ExcelUtil<SendSalary>(SendSalary.class);
        return util.exportExcel(list, "工资发放数据");
    }

    /**
     * 新增工资发放
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存工资发放
     */
    @Log(title = "工资发放", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SendSalary sendSalary, String employeeIds)
    {
        System.out.println(employeeIds);
        String[] Ids = employeeIds.split(",");
        boolean flag = true;
        Date currentDate = new Date();
        for (String id : Ids) {
            SendSalary s = new SendSalary();
            s.setEmployeeId((long) Integer.parseInt(id));
            s.setState("1");
            s.setDate(currentDate);
            flag = (sendSalaryService.insertSendSalary(s) == 1);
        }
        return toAjax(flag);
    }

    /**
     * 修改工资发放
     */
    @RequiresPermissions("system:sendSalary:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SendSalary sendSalary = sendSalaryService.selectSendSalaryById(id);
        mmap.put("sendSalary", sendSalary);
        return prefix + "/edit";
    }

    /**
     * 修改保存工资发放
     */
    @RequiresPermissions("system:sendSalary:edit")
    @Log(title = "工资发放", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SendSalary sendSalary)
    {
        return toAjax(sendSalaryService.updateSendSalary(sendSalary));
    }

    /**
     * 删除工资发放
     */
    @RequiresPermissions("system:sendSalary:remove")
    @Log(title = "工资发放", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        System.out.println(ids);
        String[] Ids = ids.split(",");
        Date currentDate = new Date();
        for (String id : Ids) {
            Expend expend = new Expend();
            SendSalary sendSalary = sendSalaryService.selectSendSalaryById((long)Integer.parseInt(id));
            TeaShopEmployee employee = teaShopEmployeeService.selectTeaShopEmployeeById(sendSalary.getEmployeeId());
            expend.setType("1");
            long cost = (employee.getPosition().equals("1") ? 6000 : 10000);
            expend.setCost(cost);
            expend.setDescription((employee.getPosition().equals("1") ? "服务员" : "餐厅经理") + employee.getName() + "工资已发放");
            expend.setDate(currentDate);
            expendService.insertExpend(expend);
        }
        return toAjax(sendSalaryService.deleteSendSalaryByIds(ids));
    }
}
